<?php
namespace Home\Controller;
/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class WxController extends HomeController {
    function vote() {
        //$info=getWeixinUserInfo('oTWz5svFXSP2WPPM5YKXk7EqPheA','gh_009da023294d');
        if (empty($_REQUEST['token']) || empty($_REQUEST['openid'])) {
            //exit('参数错误');
        }
        $uinfo = getWeixinUserInfo($_REQUEST['openid'], $_REQUEST['token']);
        $this -> assign('uinfo', $uinfo);
        $dao = M('WxYueye');
        $key = trim(str_replace('投票', '', $_REQUEST['Content']));
        $id = I('get.id');
        $info = array();
        if ($key) {
            $map = array();
            $map['identifier'] = $key;
            $info = $dao -> where($map) -> find();
        }
        if ($id) {
            $map = array();
            $map['id'] = $id;
            $info = $dao -> where($map) -> find();
        }
        if (empty($info)) {
            $this -> redirect('Wx/vindex', $_REQUEST);
            exit();
        }
        $meta['title'] = '为【' . $info['title'] . "】加油 | 定向越野最佳人气投票";

        //$access_token = get_access_token($token);
        $this -> assign('meta', $meta);
        $this -> assign('info', $info);
        $this -> assign('token', $_REQUEST['token']);
        $this -> assign('openid', $_REQUEST['openid']);
        $this -> display();
    }

    function vlog_user($openid, $token, $access_token) {
        $dao = M('WxFollow');
        $arr = $dao -> getByOpenid($openid);
        if (!$arr['headimgurl']) {
            $uinfo = getWeixinUserInfo($openid, $token, $access_token);
            $arr['subscribe'] = $uinfo['subscribe'];
            if ($uinfo['subscribe'] != 0) {
                $arr['nickname'] = $uinfo['nickname'];
                $arr['sex'] = $uinfo['sex'];
                $arr['language'] = $uinfo['language'];
                $arr['city'] = $uinfo['city'];
                $arr['province'] = $uinfo['province'];
                $arr['country'] = $uinfo['country'];
                $arr['headimgurl'] = $uinfo['headimgurl'];
                $arr['subscribe_time'] = $uinfo['subscribe_time'];
                $arr['unionid'] = $uinfo['unionid'];
                $arr['remark'] = $uinfo['remark'];
                $arr['groupid'] = $uinfo['groupid'];
                $arr['mTime'] = 0;
            }
            $dao -> where(array('openid' => $arr['openid'])) -> save($arr);
        }
        return $arr;
    }

    function vindex() {
        if (empty($_REQUEST['token']) || empty($_REQUEST['openid'])) {
            //exit('参数错误');
        }
        $uinfo = getWeixinUserInfo($_REQUEST['openid'], $_REQUEST['token']);
        $this -> assign('uinfo', $uinfo);

        $dao = M('WxYueye');
        $lists = $dao -> order('vote DESC') -> select();
        $meta['title'] = "定向越野最佳人气投票";
        $this -> assign('token', $_REQUEST['token']);
        $this -> assign('openid', $_REQUEST['openid']);
        $this -> assign('meta', $meta);
        $this -> assign('lists', $lists);
        $this -> display();
    }

    function vlog() {
        $openid = I('post.openid');
        $token = I('post.token');
        $dao = M('WxYueyeLog');
        $id = I('post.id');
        if (time() < mktime(0, 0, 1, 11, 22, 2015) || time() > mktime(18, 0, 1, 11, 22, 2015)) {
            $msg = array();
            $msg['status'] = '0';
            $msg['msg'] = '11月22日00点整开始投票, 并于11月22号18点结束投票。';
            exit(json_encode($msg));
        }
        if (empty($openid) || empty($token)) {
            $msg = array();
            $msg['status'] = '0';
            $msg['msg'] = '请从微信公众号打开连接。';
            exit(json_encode($msg));
        }
        $uinfo = getWeixinUserInfo($openid, $token);
        if ($uinfo['subscribe'] == '0') {
            $msg = array();
            $msg['status'] = '0';
            $msg['msg'] = '需要关注微信公众号才能投票。';
            exit(json_encode($msg));
        }
        $vinfo = $dao -> getByOpenid($openid);
        if ($vinfo) {
            $info = M('WxYueye') -> where(array('id' => $vinfo['yueyeid'])) -> find();
            $msg = array();
            $msg['status'] = '0';
            $msg['msg'] = '您已经投过【' . $info['title'] . '】（' . $info['identifier'] . '）一票了，谢谢参与。';
            exit(json_encode($msg));
        } else {
            $data = array();
            $data['openid'] = $openid;
            $data['stars'] = 1;
            $data['ctime'] = time();
            $data['yueyeid'] = $id;
            $dao -> add($data);
            M('WxYueye') -> where(array('id' => $id)) -> setInc('vote', 1);
            $info = M('WxYueye') -> where(array('id' => $id)) -> find();
            $msg = array();
            $msg['status'] = '1';
            $msg['vote'] = $info['vote'];
            $msg['id'] = $info['id'];
            $msg['msg'] = '感谢您的参与，投票成功。';
            exit(json_encode($msg));
        }
    }//vlog

    function maps() {
        if (time() < mktime(8, 0, 1, 11, 22, 2015)) {
            $text = "<p>11月22日8点整可刷新查看地图。</p>";
        } else {
            $text = '<a href="/Public/Home/images/maps.jpg"><img src="/Public/Home/images/maps.jpg" title="定向越野电子任务地图" /></a>';
        }
        if (empty($_REQUEST['token']) || empty($_REQUEST['openid'])) {
            exit('参数错误');
        }
        $uinfo = getWeixinUserInfo($_REQUEST['openid'], $_REQUEST['token']);
        if ($uinfo['subscribe'] == 0) {
            exit('请关注大学加微信公众号之后查看此地图。');
        }
        $this -> assign('uinfo', $uinfo);
        $meta['title'] = "定向越野活动地图";
        $this -> assign('token', $_REQUEST['token']);
        $this -> assign('openid', $_REQUEST['openid']);
        $this -> assign('meta', $meta);
        $this -> assign('text', $text);
        $this -> display();
    }//maps

}
